﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace SF.Ucenter.tools
{
    /// <summary>
    /// centerusers_ajax 的摘要说明
    /// </summary>
    public class centerusers_ajax : IHttpHandler, IRequiresSessionState
    {
        Model.sf_user_info userInfo;
        string appWhere;
        BLL.sf_user_info userBll = new BLL.sf_user_info();
        BLL.sf_manager_scale scaleBll = new BLL.sf_manager_scale();
        BLL.sf_user_withdraw_order cashBll = new BLL.sf_user_withdraw_order();
        BLL.sf_user_bank bankBll = new BLL.sf_user_bank();
        BLL.sf_goods_order orderInfoBll = new BLL.sf_goods_order();
        public void ProcessRequest(HttpContext context)
        {
            /*验证用户是否登录*/
            if (context.Session[DTKeys.SESSION_ADMIN_INFO] as Model.sf_user_info == null)
            {
                context.Response.Redirect("/mycenterusers/index.aspx");
            }
            userInfo = context.Session[DTKeys.SESSION_ADMIN_INFO] as Model.sf_user_info;
            appWhere = Utils.getWhereByInfo(userInfo.busnieseNum, userInfo.appNum, userInfo.templatesNum);
            string action = context.Request.QueryString["action"];
            switch (action)
            {
                case "get_center_msg": //获取 个人中心 所需信息
                    get_center_msg(context);
                    break;
                case "get_mywithdraw_msg": //获取 我的佣金 所需信息
                    get_mywithdraw_msg(context);
                    break;
                case "get_withdraw_payway": //获取当前支付方式
                    get_withdraw_payway(context);
                    break;
                case "apply_withdraw": //申请提现
                    apply_withdraw(context);
                    break;
            }
        }


        /// <summary>
        /// 获取当前支付方式
        /// 根据最近时间添加/上次提现方式
        /// </summary>
        /// <param name="context"></param>
        private void get_withdraw_payway(HttpContext context)
        {
            Model.sf_user_withdraw_order cash = cashBll.GetModel("userNum='" + userInfo.userNum + "' and " + appWhere + " order by id desc");
            Model.sf_user_bank bank = new Model.sf_user_bank();
            if (cash != null)
            {
                bank = bankBll.GetModel("bankNum='" + cash.bankNum + "' and userNum='" + userInfo.userNum + "' and " + appWhere);
            }
            else
            {
                bank = bankBll.GetModel("userNum='" + userInfo.userNum + "' and " + appWhere + " order by id desc");
            }
            string str = bank == null ? "" : "^" + bank.personName + "_" + bank.phone + "_" + bank.bankNumber + "_" + bank.bankNum;
            context.Response.Write(str);
        }

        /// <summary>
        /// 申请提现
        /// </summary>
        /// <param name="context"></param>
        private void apply_withdraw(HttpContext context)
        {
            try
            {
                Model.sf_user_withdraw_order model = new Model.sf_user_withdraw_order();
                string payNum = context.Request.QueryString["payid"];
                string money = context.Request.QueryString["money"];
                model.money = Convert.ToDecimal(money);
                model.busnieseNum = userInfo.busnieseNum;
                model.appNum = userInfo.appNum;
                model.templatesNum = userInfo.templatesNum;
                model.orderId = "TX-" + Utils.GetRamCode();
                model.addTime = DateTime.Now;
                model.bankNum = payNum;
                model.status = 0;
                model.userNum = userInfo.userNum;
                string res = "";
                res = cashBll.Add(model) > 0 ? "true" : "";
                context.Response.Write(res);
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 获取 我的佣金 所需信息
        /// </summary>
        /// <param name="context"></param>
        private void get_mywithdraw_msg(HttpContext context)
        {
            //一、二、三级订单数量
            double oneScale = 0, twoScale = 0, threeScale = 0;
            DataTable dsc = scaleBll.GetList(appWhere).Tables[0];
            foreach (DataRow r in dsc.Rows)
            {
                if (r["levels"].ToString() == "1")
                {
                    oneScale = Convert.ToDouble(r["scale"]);
                }
                else if (r["levels"].ToString() == "2")
                {
                    twoScale = Convert.ToDouble(r["scale"]);
                }
                else if (r["levels"].ToString() == "3")
                {
                    threeScale = Convert.ToDouble(r["scale"]);
                }
            }
            //一、二、三级佣金
            double moneyCount = 0, oneMoney = 0, twoMoney = 0, threeMoney = 0;
            //佣金、已提现金额
            double myMoney = 0, tempMoeny = 0, getMoney = 0;
            DataTable dt1 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 1,appWhere);
            foreach (DataRow r in dt1.Rows)
            {
                tempMoeny += Convert.ToDouble(r["totalMoney"]);
            }
            oneMoney = oneScale * tempMoeny;
            tempMoeny = 0;
            DataTable dt2 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 2, appWhere);
            foreach (DataRow r in dt2.Rows)
            {
                tempMoeny += Convert.ToDouble(r["totalMoney"]);
            }
            twoMoney = oneScale * tempMoeny;
            tempMoeny = 0;
            DataTable dt3 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 3, appWhere);
            foreach (DataRow r in dt3.Rows)
            {
                tempMoeny += Convert.ToDouble(r["totalMoney"]);
            }
            threeMoney = oneScale * tempMoeny;
            DataTable dt4 = cashBll.GetList("userNum='" + userInfo.userNum + "' and "+appWhere).Tables[0];
            foreach (DataRow r in dt4.Rows)
            {
                getMoney += Convert.ToDouble(r["money"]);
            }
            moneyCount = oneMoney + twoMoney + threeMoney;
            myMoney = moneyCount - getMoney;
            string str = "^" + myMoney + "_" + getMoney + "_" + moneyCount + "_" + oneMoney + "_" + twoMoney + "_" + threeMoney;
            context.Response.Write(str);
        }


        /// <summary>
        /// 获取 东家管理 所需信息
        /// </summary>
        /// <param name="context"></param>
        private void get_center_msg(HttpContext context)
        {
            try
            {
                //一、二、三级订单数量
                double oneScale = 0, twoScale = 0, threeScale = 0;
                DataTable dsc = scaleBll.GetList(appWhere).Tables[0];
                foreach (DataRow r in dsc.Rows)
                {
                    if (r["levels"].ToString() == "1")
                    {
                        oneScale = Convert.ToDouble(r["scale"]);
                    }
                    else if (r["levels"].ToString() == "2")
                    {
                        twoScale = Convert.ToDouble(r["scale"]);
                    }
                    else if (r["levels"].ToString() == "3")
                    {
                        threeScale = Convert.ToDouble(r["scale"]);
                    }
                }
                //一、二、三级客户数量
                int userCount = 0, oneUsers = 0, twoUsers = 0, threeUsers = 0;
                //一、二、三级订单数量
                int orderCount = 0, oneOrders = 0, twoOrders = 0, threeOrders = 0;
                //推广总金额
                double allMoney = 0, myMoney = 0, oneMoney = 0, twoMoney = 0, threeMoney = 0; ;
                oneUsers = userBll.GetList(" fatherKey='" + userInfo.inviteKey + "' and "+appWhere).Tables[0].Rows.Count;
                twoUsers = userBll.getTwoUsers(userInfo.inviteKey,appWhere).Rows.Count;
                threeUsers = userBll.getThreeUsers(userInfo.inviteKey, appWhere).Rows.Count;
                userCount = oneUsers + twoUsers + threeUsers;
                DataTable dt1 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 1,appWhere);
                oneOrders = dt1.Rows.Count;
                foreach (DataRow r in dt1.Rows)
                {
                    oneMoney += Convert.ToDouble(r["totalMoney"]);
                }
                myMoney += oneScale * oneMoney;
                DataTable dt2 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 2, appWhere);
                twoOrders = dt2.Rows.Count;
                foreach (DataRow r in dt2.Rows)
                {
                    twoMoney += Convert.ToDouble(r["totalMoney"]);
                }
                myMoney += twoScale * twoMoney;
                DataTable dt3 = orderInfoBll.getLevelOrders(userInfo.inviteKey, 3, appWhere);
                threeOrders = dt3.Rows.Count;
                foreach (DataRow r in dt3.Rows)
                {
                    threeMoney += Convert.ToDouble(r["totalMoney"]);
                }
                allMoney = oneMoney + twoMoney + threeMoney;
                myMoney += threeScale * threeMoney;
                orderCount = oneOrders + twoOrders + threeOrders;
                string str = "^" + allMoney + "_" + userCount + "_" + myMoney + "_" + orderCount + "_" + oneOrders + "_" + twoOrders + "_" + threeOrders + "_" + oneMoney + "_" + twoMoney + "_" + threeMoney + "_" + oneUsers + "_" + twoUsers + "_" + threeUsers + "_1";
                if (userInfo.sellModel==0)
                {
                    str = "^0_0_0_0_0_0_0_0_0_0_0_0_0_0";
                }
                context.Response.Write(str);
            }
            catch (Exception ex)
            {
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}